#!/usr/bin/env python
#
# conversation.py
#
# Contains the Conversation class

import sys
sys.path.append('depends')

import crowd, tweet, conversationSurface
from positionMatrix import PositionMatrix
import pygame, random, math

class Conversation(object):
	"""
	Conversation stores Tweets that are associated with each other.
	"""
	posMat = PositionMatrix()
	def __init__(self, tweet):
		"""
		Construct a new conversation with its first Tweet
		"""
		# Create a tweetList with the first tweet
		self.tweetList = [tweet]

		# Change Anchor Tweet image to tree
		tweet.changeImage()
		# FIXME add profile info from first tweet to the Conversation class
		# to know who started the conversation

		tweet.rect.topleft = Conversation.posMat.getRandPosition()
		self.x = 0
		self.y = 0

		self.minRadius = 10
		self.maxRadius = 75

		self.maxIncrease = 15

		# Add new Conversation (self) to Crowd
		newCrowd = crowd.Crowd()
		newCrowd.addConversation(self)

	def addTweet(self, tweet):
		"""
		Add new Tweet to the conversation
		"""
		# Scale the anchor tweet as new tweets are added
		anchTweet = self.tweetList[0]
		if len(self.tweetList) <= 15:
			center = anchTweet.rect.center
			anchTweet.image = pygame.transform.scale(anchTweet.image, (int(anchTweet.image.get_width() * 1.1), int(anchTweet.image.get_height() * 1.1)))
			anchTweet.rect = anchTweet.image.get_rect()
			anchTweet.rect.center = center
			if len(self.tweetList) == 4:
				anchTweet.increaseTree()
			if len(self.tweetList) == 9:
				anchTweet.increaseTree()
			if len(self.tweetList) == 15:
				anchTweet.increaseTree()

		# position the new tweet
		radius = random.randint(self.minRadius, self.maxRadius)
		theta = random.random() * (2.0 * math.pi)
		
		tweetX = (math.cos(theta)*radius) + anchTweet.rect.center[0]
		tweetY = (math.sin(theta)*radius) + anchTweet.rect.center[1]
		
		tweet.rect.center = (tweetX, tweetY)

		# add the new tweet
		self.tweetList.append(tweet)

	def checkClicked(self, pos):
		checkTweet = self.tweetList[0]
		clickX = pos[0]
		clickY = pos[1]
		spriteX = checkTweet.rect.left
		spriteY = checkTweet.rect.top
		spriteWidth = checkTweet.rect.width
		spriteHeight = checkTweet.rect.height
		
		if clickX >= spriteX and clickX <= (spriteX + spriteWidth) and\
		       clickY >= spriteY and clickY <= (spriteY + spriteHeight):
			return self
		else:
			return None
		

	def update(self):
		"""
		Call the update function on individual tweets
		"""
		for tweet in self.tweetList:
			tweet.update()

	def draw(self):
		"""
		Call the draw function for each tweet in the conversation
		"""
		for tweet in self.tweetList:
			tweet.draw()

	def getConverSurface(self):
		return conversationSurface.ConversationSurface(self)
		
	def getReplySurface(self):
		return replySurface.ReplySurface(self)

	def __repr__(self):
		"""
		Represent Conversation as a string
		"""
		rprString = "("
		for tweet in self.tweetList:
			rprString += tweet.message + ","
		rprString += ")"
		return rprString
